﻿// SilverlightDesktop.Net - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop.Net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Linq;
using System.Web.UI;
using SilverlightDesktopCore.DAL;

public partial class Desktop : System.Web.UI.UserControl
{
    #region Public Properties

    private int _UserID;
    private int _DesktopID;

    public int UserID
    {
        set
        {
            _UserID = value;
        }
    }

    public int DesktopID
    {
        set
        {
            _DesktopID = value;
        }
    }
    #endregion

    public string SilverlightInitParams { get; set; }
    public string SilverlightSourceParams { get; set; }
    public string SilverlightHeightParams { get; set; }
    public string SilverlightWidthParams { get; set; }
    public string SilverlightWindowlessParams { get; set; }

    GeneralSettings GeneralSettings = new GeneralSettings();
    string AutoLoadModule = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadDesktopInstance();

        string strIPAddress = this.Context.Request.UserHostAddress;
        string strPassword = Authendication.SetSilverlightKey(_UserID, strIPAddress);
        string strWebServiceBase = GetWebServiceBase();

        // Register Silverlight.js file
        Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "SilverlightJS",
            (this.TemplateSourceDirectory + "/Silverlight.js"));

        SilverlightSourceParams = "../ClientBin/SilverlightDesktop.xap";

        SilverlightInitParams =
            String.Format("PortalID={0},ModuleId={1},UserID={2},Password={3},WebServiceBase={4},AutoLoadModule={5}",
            "-1", _DesktopID.ToString(), _UserID.ToString(), strPassword, strWebServiceBase, AutoLoadModule);

    }

    #region GetWebServiceBase
    private string GetWebServiceBase()
    {
        string strWebServiceBase = String.Format(@"{0}://", this.Request.Url.Scheme);
        strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, this.Request.Url.Host.Replace(@"\", @"/"));
        strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, ((this.Request.Url.Port == 80) ? "" : ":" + this.Request.Url.Port.ToString()));
        strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, this.Request.ApplicationPath.Replace(@"\", ""));
        strWebServiceBase = String.Format(@"{0}/webservice/", strWebServiceBase);
        return strWebServiceBase;
    }
    #endregion

    #region LoadDesktopInstance
    private void LoadDesktopInstance()
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var result = (from Instances in SilverlightDesktopDAL.SilverlightDesktopInstances
                      where Instances.ID == _DesktopID
                      select Instances).FirstOrDefault();

        if (result != null)
        {
            SilverlightHeightParams = result.DesktopSizeHorizontal.ToString();
            SilverlightWidthParams =  result.DesktopSizeVertical.ToString();
            SilverlightWindowlessParams = (result.Transparent == true) ? "<param name='windowless' value='true' />" : "";
            AutoLoadModule = result.AutoLoadModule.ToString();
        }
        else
        {
            // Load default values
            SilverlightHeightParams = "100%";
            SilverlightWidthParams = "100%";
        }
    }
    #endregion
}
